# Methods added to this helper will be available to all templates in the application.

module ApplicationHelper
  include Localization

  def ultima_actualizacion(from_time, include_seconds = false)
    if not from_time.nil? then
      "<span class=\"timestamp\">Ultima actualizacion hace " + tiempo_pasado_en_palabras(from_time, Time.now, include_seconds) + "</span>"
    end
  end

  def tiempo_pasado_en_palabras(from_time, to_time = 0, include_seconds = false)
    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time - from_time).abs)/60).round
    distance_in_seconds = ((to_time - from_time).abs).round

    case distance_in_minutes
      when 0..1
        return (distance_in_minutes==0) ? 'menos de un minuto' : '1 minuto' unless include_seconds
        case distance_in_seconds
          when 0..5   then 'menos de 5 segundos'
          when 6..10  then 'menos de 10 segundos'
          when 11..20 then 'menos de 20 segundos'
          when 21..40 then 'medio minuto'
          when 41..59 then 'menos de un minuto'
          else             '1 minuto'
        end
                            
      when 2..45      then "#{distance_in_minutes} minutos"
      when 46..90     then 'cerca de 1 hora'
      when 90..1440   then "cerca de #{(distance_in_minutes.to_f / 60.0).round} horas"
      when 1441..2880 then '1 dia'
      else                 "#{(distance_in_minutes / 1440).round} dias"
    end
  end
end
